public class HammingCode {
	
	public static int[] check(String input){
		/*Copy String to array of int*/
		int[] bits = new int[11];
		int inputpos = 0;
		//Transfer user input into array of int
		for(int bitspos = bits.length - 1; bitspos>= 0; bitspos--){
			if(bitspos != 0 && bitspos != 1 && bitspos != 3 && bitspos != 7){
				if(input.charAt(inputpos) == '1'){
					bits[bitspos] = 1;
				}
				else{
					bits[bitspos] = 0;
				}
				inputpos++;
			}		
		}
		//Insert parities into array of int
		bits = insertParities(bits);
		return bits;
	}
	
	private static int[] insertParities(int[] bits){
		int paritybit = 0;
		int setbitcount;
		//Loop through parity bits to be calculated
		while(paritybit <= 7){
			//Amount of set bits detected, relevant to this parity bit
			setbitcount = 0;
			//Loop through bits to check if set
			for(int bitcheck = 2; bitcheck<= 10;bitcheck++){
				//If current bit is relevant to this parity bit and if the bit is set
				if(bitcheck == 2 && (paritybit == 0 || paritybit == 1) && bits[bitcheck] == 1){
					setbitcount++;
				}
				if(bitcheck == 4 && (paritybit == 0 || paritybit == 3) && bits[bitcheck] == 1){
					setbitcount++;
				}
				if(bitcheck == 5 && (paritybit == 1 || paritybit == 3) && bits[bitcheck] == 1){
					setbitcount++;
				}
				if(bitcheck == 6 && (paritybit == 0 || paritybit == 1 || paritybit == 3) && bits[bitcheck] == 1){
					setbitcount++;
				}
				if(bitcheck == 8 && (paritybit == 0 || paritybit == 7) && bits[bitcheck] == 1){
					setbitcount++;
				}
				if(bitcheck == 9 && (paritybit == 1 || paritybit == 7) && bits[bitcheck] == 1){
					setbitcount++;
				}
				if(bitcheck == 10 && (paritybit == 0 || paritybit == 1 || paritybit == 7) && bits[bitcheck] == 1){
					setbitcount++;
				}
			}
			//If amt of set bits is even
			if(setbitcount % 2 != 0){
				bits[paritybit] = 1;
			}
			//Go to next parity bit
			paritybit++;
			//Ensure a parity bit has been selected
			while(paritybit != 0 && paritybit != 1 && paritybit != 3 && paritybit != 7 && paritybit <=8){
				paritybit++;
			}	
		}//While
		return bits;	
	}
}